<?php


namespace frontend\controllers;

use common\models\business\BusinessPerson;
use common\models\business\BusinessUser;
use common\models\wechat\WechatAccounts;
use common\models\wechat\WechatPaySetting;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use Yii;
use yii\web\Controller;
use yii\web\Response;

class BaseMobileController extends Controller
{
    public $user = null;
    const DEBUG = true;
    const SESSIONWX = 'wxuserinfo';




    public function init()
    {

        $model = WechatAccounts::findOne(WechatAccounts::WID);
        //设置参数
        Yii::$app->params['WECHAT']['app_id'] = $model->key;
        Yii::$app->params['WECHAT']['secret'] = $model->secret;
        Yii::$app->params['WECHAT']['token'] = $model->token;
        Yii::$app->params['WECHAT']['aes_key'] = $model->encodingaeskey;

        $payment = WechatPaySetting::findOne(['wid'=>$model->id]);
        Yii::$app->params['WECHAT']['payment'] = [
            'merchant_id' => $payment->mchid,
            'key' => $payment->apisecret,
        ];



        /*兼容老二维码*/
        //以前在muxin.tuhuokeji.com 生成的码，由于网页授权变化，所以在此做兼容，跳到新平台www.4001108008.com
//        if(\Yii::$app->request->pathInfo == 'business/pig/detail' || \Yii::$app->request->pathInfo == 'shop/site/from'){
//            $pigno = \Yii::$app->request->get('pigno');
//            $pigid = \Yii::$app->request->get('pigid');
//            if($pigno){
//                $res = (new Query())->select('id')->from('business_pig_jianrong')->where('no=:no',[':no'=>$pigno])->one();
//            }else if($pigid){
//                $res = (new Query())->select('id')->from('business_pig_jianrong')->where('no=:no',[':no'=>$pigid])->one();
//            }
//            if(!empty($res) || $res !== false){
//                header('Location:http://www.4001108008.com/'.\Yii::$app->request->url);exit;
//            }
//        }
        /*兼容end*/




        //未授权去授权
        if(!self::DEBUG && !\Yii::$app->wechat->isAuthorized()) {
            $this->WxOauth();
            $user = json_decode(\Yii::$app->session->get(\Yii::$app->wechat->sessionParam),true);
            $userinfo = BusinessUser::find()->select('id')->where('openid=:openid',[':openid'=>$user['id']])->asArray()->one();

            \Yii::$app->session->set(self::SESSIONWX,[
                'openid' => $user['id'],
                'userid' => $userinfo['id'],
                'avatar' => $user['avatar'],
                'nickname' => $user['nickname'],
            ]);
        }


        if((self::DEBUG) && empty($this->user)) {
            $oepnid = 'onh_81cADLOwdaEvBLoM_4_tdrjg';
            $user = BusinessUser::find()->select('id')->where('openid=:openid',[':openid'=>$oepnid])->asArray()->one();
            \Yii::$app->session->set(self::SESSIONWX,[
                'nickname' => '仔仔',
                'avatar' => 'http://wx.qlogo.cn/mmopen/47CicbLQOxtWoYIWeEQXzAXeicsEUO3wxEF0IRjQgQNm41K2skIegWuMMhh4icDrcWqleqDcyf7w98gGKe8DS8At8ZQSQ4W8J01/0',
                'access_token' => 'token',
                'openid' => $oepnid,
                'userid' => $user['id'],
            ]);
        }

        $this->user =  \Yii::$app->session->get(self::SESSIONWX);

        parent::init(); // TODO: Change the autogenerated stub
    }





    public function WxOauth(){
        if(\Yii::$app->wechat->isWechat && !\Yii::$app->wechat->isAuthorized()) {
            return \Yii::$app->wechat->authorizeRequired(['snsapi_userinfo'])->send();
        }
    }



    /**
     * @param string $msg
     * @param int $status
     * @param array $params
     * @return array
     * 回复消息
     */
    public function jsonMsg($msg='操作成功',$status=0,$params = ['url' => ''])
    {
        \Yii::$app->response->format = Response::FORMAT_JSON;
        return array_merge(['msg' => $msg,'status'=>$status],$params);
    }


    /**
     * @param $content
     * @param $url
     * @param string $title
     * @return string
     * 成功提示
     */
    public function success($content,$url,$title='温馨提示')
    {
        return $this->render('@template/common/tip/success.html',[
            'title' => $title,
            'content' => $content,
            'url' => $url
        ]);
    }

    /**
     * @param $content
     * @param $url
     * @param string $title
     * @return string
     * 错误提示
     */
    public function error($content,$url='',$title='温馨提示')
    {
        return $this->render('@template/common/tip/error.html',[
            'title' => $title,
            'content' => $content,
            'url' => $url
        ]);
    }



    /**
     * @param $content
     * @param $url
     * @param int $time
     * @param string $title
     * @return string
     * 定时关闭
     */
    public function timealert($content,$url='',$time=2000,$title='温馨提示')
    {
        return $this->render('@template/common/tip/timealert.html',[
            'title' => $title,
            'content' => $content,
            'url' => $url,
            'time' => $time

        ]);
    }











}


